package com.msb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.mapper.FcEstateMapper;
import com.msb.pojo.*;
import com.msb.response.R;
import com.msb.service.FcEstateService;
import com.msb.service.TblCompanyService;
import com.msb.vo.CellMessage;
import com.msb.vo.UnitMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼盘信息 前端控制器
 * </p>
 *
 * @author chshh
 * @since 2022-10-20
 */
@RestController
public class FcEstateController {

    @Autowired
    private TblCompanyService companyService;

    @Resource
    private FcEstateService fcEstateService;

    @RequestMapping("/estate/selectCompany")
    public R selectCompany(){
        System.out.println("selectCompany");
        List<TblCompany> companies = companyService.list();
        return new R(200,"success",companies);
    }

    @RequestMapping("/estate/insertEstate")
    public R insertEstate(FcEstate fcEstate){
        //判断是否存在相同的住宅编码
        String result ="-1";
        QueryWrapper<FcEstate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("estate_code",fcEstate.getEstateCode());
        List<FcEstate> list = fcEstateService.list(queryWrapper);
        if (list.size() == 0){
            boolean save = fcEstateService.save(fcEstate);
            if (save){ result = "1"; }else {result = "-1";}
            return new R(200,result,save);
        }else return new R(200,result,"房产编码已经存在");
    }


    @RequestMapping("/estate/selectBuilding")
    public R selectBuilding(Integer buildingNumber,String estateCode){
        return fcEstateService.selectBuilding(buildingNumber,estateCode);
    }

    @RequestMapping("/estate/updateBuilding")
    public R updateBuilding(FcBuilding fcBuilding){
        Integer i = fcEstateService.updateBuilding(fcBuilding);
        if (i==1){return new R(200,"i","成功");}
        return new R(200,"i","失败");
    }

    @RequestMapping("/estate/selectUnit")
    public R selectUnit(@RequestBody UnitMessage[] unitMessages){
        System.out.println("selectUnit");
        List<FcUnit> allUnit = new ArrayList<>();
        for (UnitMessage unitMessage : unitMessages) {
            allUnit.addAll(fcEstateService.selectUnit(unitMessage));
        }
        System.out.println("allUnit = " + allUnit);
        return new R(200,"success",allUnit);
    }

    @RequestMapping("/estate/updateUnit")
    public R updateUnit(FcUnit fcUnit){
        System.out.println("updateUnit");
        Integer result = fcEstateService.updateUnit(fcUnit);
        if(result == 1){
            return new R("更新数据成功！");
        }
        return new R("更新数据失败！");
    }

    @RequestMapping("/estate/insertCell")
    public R insertCell(@RequestBody CellMessage[] cellMessages){
        System.out.println("insertCell");
        List<FcCell> allFcCell = new ArrayList<>();
        for (CellMessage cellMessage : cellMessages) {
            allFcCell.addAll(fcEstateService.insertCell(cellMessage));
        }
        return new R(allFcCell);
    }

    @RequestMapping("/estate/selectBuildingByEstate")
    public R selectBuildingByEstate(String estateCode){
        System.out.println("selectBuildingByEstate");
        List<FcBuilding> fcBuildings = fcEstateService.selectBuildingByEstate(estateCode);
        return new R(fcBuildings);
    }

    @RequestMapping("/estate/selectUnitByBuildingCode")
    public R selectUnitByBuildingCode(String buildingCode){
        System.out.println("selectUnitByBuildingCode");
        List<FcUnit> fcUnits = fcEstateService.selectUnitByBuildingCode(buildingCode);
        return new R(fcUnits);
    }

    @RequestMapping("/estate/selectCell")
    public R selectCell(String unitCode){
        System.out.println("selectCell");
        List<FcCell> fcCells = fcEstateService.selectCell(unitCode);
        return new R(fcCells);
    }

    @RequestMapping("/estate/selectEstate")
    public R selectEstate(String company){
        System.out.println("selectEstate");
        List<FcEstate> estates = fcEstateService.selectEstate(company);
        return new R(estates);
    }

    @RequestMapping("/estate/selectAllEstate")
    public R selectAllEstate(){
        System.out.println("selectAllEstate");
        List<FcEstate> fcEstates = fcEstateService.selectAllEstate();
        return new R(fcEstates);
    }

    @RequestMapping("/estate/selectBuildingByEstateCode")
    public R selectBuildingByEstateCode(String estateCode){
        System.out.println("selectBuildingOne"+estateCode);
        List<FcBuilding> fcBuildings = fcEstateService.selectBuildingByEstateCode(estateCode);
        return new R(fcBuildings);
    }

    @RequestMapping("/estate/selectBuildingByEstateCodeAndBuildingCode")
    public R selectBuildingByEstateCodeAndBuildingCode(@RequestParam("estateCode") String estateCode,
                                                       @RequestParam("buildingCode") String buildingCode){
        System.out.println("selectBuildingByEstateCodeAndBuildingCode");
        FcBuilding fcBuilding = fcEstateService.selectBuildingByEstateCodeAndBuildingCode(estateCode,buildingCode);
        return new R(fcBuilding);
    }


}

